package com.example.cwj11_20;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

import static java.lang.Thread.interrupted;

public class QQmusicActivity extends AppCompatActivity implements View.OnClickListener{
    SeekBar seekBar;
    Button btn_play;
    Button btn_replay;
    Button btn_tzplay;
    MediaPlayer player;
    private Thread mThread;
    Handler handler = new Handler(){
      @Override
        public void handleMessage(Message msg){
          switch (msg.what){

              case 100:
                  int currentPosition = (Integer) msg.obj;
                    seekBar.setProgress(currentPosition);
                    break;
          }
      }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qqmusic);
        seekBar = findViewById(R.id.sk_music);
        btn_play = findViewById(R.id.btn_play);
        btn_play.setOnClickListener(this);
        btn_replay = findViewById(R.id.btn_replay);
        btn_replay.setOnClickListener(this);
        btn_tzplay = findViewById(R.id.btn_tzplay);
        btn_tzplay.setOnClickListener(this);

    }
    public void initSeekbar(){
       int musicWidth=player.getDuration();
       System.out.println("---musicwidth---"+ musicWidth);
       seekBar.setMax(musicWidth);
    }

    @Override
    public void onClick(View view) {
        int viewld = view.getId();
        switch (viewld){
            case R.id.btn_play:
                System.out.println("---点击了播放按钮---");
                if (player == null) {
                    player = MediaPlayer.create(QQmusicActivity.this, R.raw.music1);
                    initSeekbar();
                    btn_play.setText("暂停");
                    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            if (mThread != null) {
                                mThread.interrupt();
                            }
                            Toast.makeText(QQmusicActivity.this, "播放完毕", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });
                    player.start();
                    updatePrpgress();
                }else if (player.isPlaying()){
                    player.pause();
                    btn_play.setText("播放");
                }else {
                    player.start();
                    btn_play.setText("暂停");
                }
            break;

            case  R.id.btn_replay:
                System.out.println("---点击了重播按钮---");
                if (player!=null){
                    player.seekTo(0);
                }
                break;
            case R.id.btn_tzplay:
                System.out.println("---点击了停止按钮---");
                if (player!=null){
                    player.stop();
                    player.release();
                    player=null;
                    if (mThread!=null){
                        mThread.interrupt();//中断音乐位置的线程
                    }
                }
                break;

        }
    }
    public void updatePrpgress(){
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!interrupted()){
                    Message message = Message.obtain();
                    int currentPosition = getCurrentPosition();
                    Message message1 =Message.obtain();
                    message.what = 100;
                    message.obj = currentPosition;
                    handler.sendMessage(message);
                }
            }
        });
        mThread.start();
    }
    public int getCurrentPosition(){
        try {
            if (player !=null){
                return player.getCurrentPosition();
            }
        }catch (Exception e){
            return 0;
        }
        return 0;
    }
}
